window.addEventListener("load",function(e) {
    var selectedWord = "";

    document.getElementById("contentAreaContextMenu").addEventListener("popupshowing",function(event) {
        selectedWord = Utils.getSelection(document.popupNode);
        document.getElementById("menuitem_flashcard_add").hidden = (selectedWord.length == 0);
        document.getElementById("separator_flashcard").hidden = (selectedWord.length == 0);

        document.getElementById("menuitem_flashcard_add").setAttribute("label", '把" ' + selectedWord + ' "加入我的单词本');
    },
    false);

    document.getElementById("menuitem_flashcard_add").addEventListener("click",function(event) {
        Utils.log("Response to menu clicked.");
		
		/*
			Spike: manipluate content html in firefox plugin.
		*/
		// var notification = gBrowser.contentDocument.createElement("div");
		// notification.innerHtml = "hahah";
		// notification.style.position = "fixed";
		// notification.style.padding = "10px";
		// notification.style.backgroundColor = "red";
		// notification.style.bottom = "0 ";
		// notification.style.zIndex = "99 ";
		// notification.style.width = "200px ";
		// notification.style.height = "40px ";
		// notification.style.right = "10px ";
		// notification.style.overflow = "visible ";
		// gBrowser.contentDocument.body.appendChild(notification);

        if (SinaWeiBo.isLogin()) {
            var result = SinaWeiBo.postMessage(selectedWord + "@单词本");
            PopupNotifications.show(gBrowser.selectedBrowser, "flashcard-add",
            '"' + selectedWord + '" 已经成功加入你的单词本',
            null,
            {
                label: "确定",
                accessKey: "D",
                callback: function() {
                    }
            },
            [
            {
                label: "Reset",
                accessKey: "R",
                callback: function() {
                    Browser.Preferences.clearUserPref("oauth.access_token");
                    Browser.Preferences.clearUserPref("oauth.access_token_secret");
                }
            },
            ]);
            return;
        }

        showLoginDialog();

    },
    false);
},
false);

function showLoginDialog() {
    PopupNotifications.show(gBrowser.selectedBrowser, "flashcard-add",
    '登陆到您的新浪微博。',
    null,
    {
        label: "登陆",
        accessKey: "D",
        callback: function() {
            window.openDialog("chrome://flashcard/content/login.xul", "chrome, dialog");
        }
    },
    null);
}

function login() {
    var name = document.getElementById("txt_flashcard_name").value;
    var password = document.getElementById("txt_flashcard_password").value

    return SinaWeiBo.login(name, password);
}
